<?php
/**
 * This is a Data Model class
 */
class Data_model extends CI_Model {

	private $table = '';

	public function __construct() {
		$this->load->database();
	}

	
	function set_table($table) {
		$this->table = $table;
	}


	// --------------------------------------------------------------------
	/**
	 * add data
	 */
	public function add_data($data = '', $table = '') {
		$table = ($table == '' ? $this->table : $table);
		if(is_array($data) && count($data) > 0) {
			$this->db->insert($table, $data);
			return $this->db->insert_id();
		}
		return FALSE;
	}


	// --------------------------------------------------------------------
	/**
	 * delete data
	 */
	public function delete_data($id, $ids_data, $table = '') {
		$table = ($table == '' ? $this->table : $table);
		if(is_array($ids_data)) {
			$this->db->where_in($id, $ids_data);
		} else {
			$this->db->where($id, $ids_data);
		}
		$this->db->delete($table);
	}


	// --------------------------------------------------------------------
	/**
	 * update data
	 */
	public function update_data($where_data, $data, $table = '') {
		$table = ($table == '' ? $this->table : $table);
		if(!empty($where_data)) {
			$this->db->where($where_data);
		}
		$this->db->update($table, $data);
	}


	// --------------------------------------------------------------------
	/**
	 * get single data
	 */
	function get_single_data($where_data = '', $table = ''){
		$table = ($table == '' ? $this->table : $table);
		if($where_data){
			$this->db->where($where_data);
		}
		return $this->db->get($table)->row_array();
	}


	// --------------------------------------------------------------------
	/**
	 * get data
	 */
	function get_data($where_data = array(), $like_data = '', $order = '', $page_no = 0, $page_size = 0, $table = ''){
		$table = ($table == '' ? $this->table : $table);
		if($like_data) {
			$this->db->like($like_data);
		}
		if($order) {
			$this->db->order_by($order);
		}
		if($page_no > 0 && $page_size > 0) {
			$limit = ($page_no - 1) * $page_size;
			$query = $this->db->get_where($table, $where_data, $page_size, $limit);
		} else {
			$query = $this->db->get_where($table, $where_data);
		}
		if(is_object($query)) {
			return $query->result_array();
		}
		return array();
	}
	

	// --------------------------------------------------------------------
	/**
	 * get single
	 */
	function get_single($where_data = '', $table = ''){
		$table = ($table == '' ? $this->table : $table);
		$query = $this->db->get_where($table, $where_data);
		if(is_object($query)) {
			return $query->row_array();
		}
		return array();
	}


	// --------------------------------------------------------------------
	/**
	 * get data num
	 */
	function get_data_num($where_data = '', $like_data = '', $table = ''){
		$table = ($table == '' ? $this->table : $table);
		if($where_data) {
			$this->db->where($where_data);
		}
		if($like_data) {
			$this->db->like($like_data);
		}
		return $this->db->count_all_results($table);
	}


	// --------------------------------------------------------------------
	/**
	 * get data num in where
	 */
	function get_data_num_where_in($field = '', $where_in = array(), $table = ''){
		$table = ($table == '' ? $this->table : $table);
		if($field && is_array($where_in)) {
			$this->db->where_in($field, $where_in);
		}
		return $this->db->count_all_results($table);
                
	}
        
        
        
        
        
        function get_oci_data($where_data = array(), $like_data = '', $order = '', $page_no = 0, $page_size = 0, $table = ''){
		$table = ($table == '' ? $this->table : $table);
		if($like_data) {
			$this->db->like($like_data);
		}
		if($order) {
			$this->db->order_by($order);
		}
		if($page_no > 0 && $page_size > 0) {
			$limit = ($page_no - 1) * $page_size;
			$query = $this->db->get_where($table, $where_data, $page_size, $limit);
		} else {
			$query = $this->db->get_where($table, $where_data);
		}
		if(is_object($query)) {
			return $query->result_array();
		}
		return array();
                $this->db->close();
	}
         function get_oci_select($table = ""){
             $query = $this->db->get();
             return $query->result_array();
             $this->db->close();
         }

}